/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elookinto.blackhat;

import static com.elookinto.blackhat.SeliumEx.driver;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class SeliumBaike1 {

    static WebElement connections;
    static WebDriver driver;
    static boolean readOnly = false;
    public static void main(String[] args) throws Exception {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        //File file = new File("C:/projs/iedriverserver.exe");
        //System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
        //System.setProperty("webdriver.chrome.driver","C:\\tools\\chromedriver.exe"); 
        driver = new FirefoxDriver();
       // driver.get("http://baike.baidu.com/view/1.htm");
        String baseUrl = "http://www.microsofttranslator.com/BV.aspx?a=http%3A%2F%2Fbaike.baidu.com%2Fview%2F10696.htm";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(baseUrl);
        WebElement we = driver.findElement(By.id("content"));
        
        System.out.println(we.getText());
        
        //driver.quit();
    }

    static void sending(String email) throws Exception {
        Connection con = null;
        try {
            con = Util.getCon();
            PreparedStatement st = con.prepareStatement("SELECT k.url, k.id from linkedin as k left join sent s  on k.id = s.refid  where s.invite_time is  null and (  email is null)");
            //st.setString(1, email);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                String url = rs.getString(1);
                int refid = rs.getInt(2);
                try {
                    forUrl(url, email, refid);
                   if(!readOnly) Thread.currentThread().sleep(15000);
                } catch (Exception urle) {
                    urle.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.close();
        }
    }

    static void forUrl(String url, String email, int refid) throws Exception {
        driver.get(url);
        Connection con = null;
        try {
            con = Util.getCon();
            PreparedStatement ps = con.prepareStatement("INSERT INTO sent (id, email, refid, invite_time) values(null, ?, ?, current_timestamp) ");
            ps.setString(1, email);
            ps.setInt(2, refid);
            if (!readOnly) ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.close();
        }
        //Robot robot = new Robot();
        Actions act = new Actions(driver);
        try {
            WebElement mainMenu = driver.findElement(By.className("connect-menu-toggle"));
            act.moveToElement(mainMenu).build().perform();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            WebElement connMenu = driver.findElement(By.linkText("Connect"));
           //if (true) return;
           if(!readOnly) connMenu.click();
            
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
      
        driver.findElement(By.id("IF-reason-iweReconnect")).click();
        driver.findElement(By.id("greeting-iweReconnect")).clear();
        driver.findElement(By.id("greeting-iweReconnect")).sendKeys("I'd like to add you to my professional network on LinkedIn.\n\n- Steve from Elookinto");
        driver.findElement(By.name("iweReconnectSubmit")).click();
        
    }
}